/**
 * Created by Elsa on 2017/8/22.
 */
layui.define(['jquery', 'layer','paging', 'code','form','element','upload'], function(exports){
    var form = layui.form();
    var index;

    layui.code();
    var $ = layui.jquery,
        paging = layui.paging(),
        layer = layui.layer,
        upload = layui.upload,
        element = layui.element(); //Tab的切换功能，切换事件监听等，需要依赖element模块


    paging.init({
        url: contextPath + "/project/ops/history", //地址
        elem: '#con', //内容容器
        type: 'GET',
        //new
        key: '#s_type',
        value: '#s_value',
        params: { //发送到服务端的参数
        },
        tempElem: '#conTemp', //模块容器
        pageConfig: { //分页参数配置
            elem: '#page', //分页容器
            pageSize: 10 //分页大小
        },
        success: function() { //渲染成功的回调
            $.ajax({
                type:"GET",
                url:contextPath+"/personal/role",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    console.log(data.role);
                    if(data.role=="验船师"){
                        $('.operalog').show();
                    }
                },
                error: function (e) {

                }
            });
        },
        fail: function(msg) { //获取数据失败的回调
        },
        complate: function() { //完成的回调

        },
    });

    $("body").on("click",".operalog",function(){
        var rgstnum = $(this).data('id');

        parent.layui.element().tabAdd('demo', {
            title: rgstnum+' 日志列表',
            content:'<iframe frameborder=0 width="100%" height="700px" marginheight=0 marginwidth=0 scrolling=auto src=OperaInspection/LogList/OperaLogList.html?rgstnum='+rgstnum+'></iframe>',
            id: 'log'+ rgstnum
        });
        parent.layui.element().tabChange('demo', 'log'+ rgstnum);
        parent.layui.element().init();

    });

    $("body").on("click",".operadoc",function(){
        var rgstnum = $(this).data('id');

        parent.layui.element().tabAdd('demo', {
            title: rgstnum+' 文档列表',
            content:'<iframe frameborder=0 width="100%" height="700px" marginheight=0 marginwidth=0 scrolling=auto src=OperaInspection/DocList/OperaDocList.html?rgstnum='+rgstnum+'></iframe>',
            id: 'doc'+ rgstnum
        });
        parent.layui.element().tabChange('demo', 'doc'+ rgstnum);
        parent.layui.element().init();

    });

    $("body").on("click",".operarecord",function(){
        var rgstnum = $(this).data('id');

        console.log($("li[lay-id='"+ rgstnum +"']").length);

        parent.layui.element().tabAdd('demo', {
            title: rgstnum+' 效用实验记录',
            content:'<iframe frameborder=0 width="100%" height="700px" marginheight=0 marginwidth=0 scrolling=auto src=OperaInspection/ExperRecord/OperaExperRecord.html?rgstnum='+rgstnum+'></iframe>',
            id: 'flag'+ rgstnum
        });
        parent.layui.element().tabChange('demo', 'flag'+ rgstnum);
        parent.layui.element().init();
    });

    $("body").on("click",".operaquality",function(){
        var rgstnum = $(this).data('id');

        $.ajax({
            type: "GET",
            url: contextPath + "/project/ops/qcc/" + rgstnum,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                console.log(data);
                if(data.qccdate){
                    $.get('OperaQualityDetails.html',function(OperaQualityDetails) {
                        index = layer.open({
                            type: 1,
                            title: '质量控制表',
                            content: OperaQualityDetails,
                            area: ['auto', '600px'],
                            maxmin: false,
                            success:function (layero,index) {

                                $('input[name="shipname"]').attr("value",data.shipname);
                                $('input[name="rgstnum"]').attr("value",data.rgstnum);
                                $('input[name="qccdate"]').attr("value",data.qccdate);
                                $('input[name="qccname"]').attr("value",data.qccname);
                                $('textarea[name="test3"]').val(data.test3);
                                if(data.cdopn){
                                    $('input[name="cdopn"]').attr("value",data.cdopn);
                                    $('input[name="cdopndate"]').attr("value",data.cdopndate);
                                }

                                if(data.test1){
                                    $('input[name="test1"]').attr("checked",true);
                                }else{
                                    $('input[name="test1"]').attr("checked",false);
                                }
                                if(data.test2d1){
                                    $('input[name="test2d1"]').attr("checked",true);
                                }else{
                                    $('input[name="test2d1"]').attr("checked",false);
                                }
                                if(data.test2d2){
                                    $('input[name="test2d2"]').attr("checked",true);
                                }else{
                                    $('input[name="test2d2"]').attr("checked",false);
                                }
                                if(data.test2d3){
                                    $('input[name="test2d3"]').attr("checked",true);
                                }else{
                                    $('input[name="test2d3"]').attr("checked",false);
                                }
                                if(data.test2d4){
                                    $('input[name="test2d4"]').attr("checked",true);
                                }else{
                                    $('input[name="test2d4"]').attr("checked",false);
                                }
                                if(data.test2d5){
                                    $('input[name="test2d5"]').attr("checked",true);
                                }else{
                                    $('input[name="test2d5"]').attr("checked",false);
                                }
                                if(data.test2d6){
                                    $('input[name="test2d6"]').attr("checked",true);
                                }else{
                                    $('input[name="test2d6"]').attr("checked",false);
                                }
                                if(data.test2d7){
                                    $('input[name="test2d7"]').attr("checked",true);
                                }else{
                                    $('input[name="test2d7"]').attr("checked",false);
                                }
                                if(data.test2d8){
                                    $('input[name="test2d8"]').attr("checked",true);
                                }else{
                                    $('input[name="test2d8"]').attr("checked",false);
                                }
                                if(data.test2d9){
                                    $('input[name="test2d9"]').attr("checked",true);
                                }else{
                                    $('input[name="test2d9"]').attr("checked",false);
                                }
                                if(data.test2d10){
                                    $('input[name="test2d10"]').attr("checked",true);
                                }else{
                                    $('input[name="test2d10"]').attr("checked",false);
                                }

                                $('#su').on('click',function () {
                                    window.open('../../../print/opera/OperaQualityTable/OperaQualityTable.html?rgstnum='+rgstnum);
                                });
                            },
                            yes: function(index) {
                                console.log(index);
                            },
                            full: function(elem) {
                                var win = window.top === window.self ? window : parent.window;
                                $(win).on('resize', function() {
                                    var $this = $(this);
                                    elem.width($this.width()).height($this.height()).css({
                                        top: 0,
                                        left: 0
                                    });
                                    elem.children('div.layui-layer-content').height($this.height() - 95);
                                });
                            }
                        });
                        form.render();
                    });
                } else {
                    layer.msg('请新建质量控制表...',{time: 1000})
                }

            },
            error: function (e) {

            }
        });
    });

    $("body").on("click",".operamoress",function(){
        var rgstnum = $(this).data('id');

        $.ajax({
            type: "GET",
            url: contextPath + "/project/ops/" + rgstnum,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                console.log(data);
                $.get('OperaDetailsForm.html',function(OperaDetailsForm) {
                    index = layer.open({
                        type: 1,
                        title: '审核申请表',
                        content: OperaDetailsForm,
                        area: ['auto', '600px'],
                        maxmin: false,
                        success:function (layero,index) {
                            if(data.info1){
                                $('input[name="info1"]').attr("checked",true);
                                $('input[name="info1num"]').css("visibility",'visible');
                                $('#download1').css("visibility",'visible');
                                $('#download1').attr('href',contextPath+'/download?file='+data.info1adr);
                            }else{
                                $('input[name="info1"]').attr("checked",false);
                            }
                            if(data.info2){
                                $('input[name="info2"]').attr("checked",true);
                                $('input[name="info2num"]').css("visibility",'visible');
                                $('#download2').css("visibility",'visible');
                                $('#download2').attr('href',contextPath+'/download?file='+data.info2adr);
                            }else{
                                $('input[name="info2"]').attr("checked",false);
                            }
                            if(data.info3){
                                $('input[name="info3"]').attr("checked",true);
                                $('#download3').css("visibility",'visible');
                                $('#download3').attr('href',contextPath+'/download?file='+data.info3adr);
                            }else{
                                $('input[name="info3"]').attr("checked",false);
                            }
                            if(data.info4){
                                $('input[name="info4"]').attr("checked",true);
                                $('input[name="info4name"]').css("visibility",'visible');
                                $('#download4').css("visibility",'visible');
                                $('#download4').attr('href',contextPath+'/download?file='+data.info4adr);
                            }else{
                                $('input[name="info4"]').attr("checked",false);
                            }

                            $('input[name="shipname"]').attr("value",data.shipname);
                            $('input[name="insptype"]').attr("value",data.insptype);
                            $('input[name="argstnum"]').attr("value",data.rgstnum);
                            $('input[name="inspnum"]').attr("value",data.inspnum);
                            $('input[name="shiplength"]').attr("value",data.shiplength);
                            $('input[name="grosston"]').attr("value",data.grosston);
                            $('input[name="hostpower"]').attr("value",data.hostpower);
                            $('input[name="region"]').attr("value",data.region);
                            $('input[name="shipstops"]').attr("value",data.shipstops);
                            $('input[name="shiptype"]').attr("value",data.shiptype);
                            $('input[name="inspaddress"]').attr("value",data.inspaddress);
                            $('input[name="dockingdate"]').attr("value",data.dockingdate);
                            $('input[name="apnttime"]').attr("value",data.apnttime);
                            $('input[name="cdinfo"]').attr("value",data.cdname+'/'+data.cdusername);
                            $('input[name="info4name"]').attr("value",data.info4name);
                            $('textarea[name="additional"]').val(data.additional);
                            $('input[name="apunitname"]').attr("value",data.apunitname);
                            $('input[name="fax"]').attr("value",data.fax);
                            $('input[name="zipcode"]').attr("value",data.zipcode);
                            $('input[name="crecode"]').attr("value",data.crecode);
                            $('input[name="writedate"]').attr("value",data.writedate);
                            $('input[name="ccadr"]').attr("value",data.ccadr);
                            $('input[name="writename"]').attr("value",data.writename);
                            var ccstr = data.ccinfo.split('/');
                            $('input[name="ccinfo"]').attr("value",ccstr[0]);
                            $('input[name="ccphone"]').attr("value",ccstr[1]);
                            $('textarea[name="remarks"]').val(data.remarks);

                            if(data.auditstate){
                                $('input[name="auditstate"]').attr("value",data.auditstate);
                                $('input[name="auditdate"]').attr("value",data.auditdate);
                                if(data.auditstate == "不予受理"){
                                    $('textarea[name="auditopn"]').show();
                                    $('textarea[name="auditopn"]').val(data.auditopn);
                                } else{
                                    $('.operationGroup').show();
                                    if(data.headman==1){
                                        $('input:radio[name="headman1"][value="1"]').attr("checked",true);
                                        $('input:radio[name="headman2"][value="1"]').attr("checked",true);
                                    }else if(data.headman==2){
                                        $('input:radio[name="headman1"][value="2"]').attr("checked",true);
                                        $('input:radio[name="headman2"][value="2"]').attr("checked",true);
                                    }else if(data.headman==3){
                                        $('input:radio[name="headman1"][value="3"]').attr("checked",true);
                                        $('input:radio[name="headman2"][value="3"]').attr("checked",true);
                                    }else{
                                    }
                                    $('input[name="ycs1info"]').attr("value",data.ycs1name+"/"+data.ycs1username);
                                    $('input[name="ycs2info"]').attr("value",data.ycs2name+"/"+data.ycs2username);
                                    $('input[name="ycs3info"]').attr("value",data.ycs3name+"/"+data.ycs3username);
                                }
                            }else{
                                $('.reviewResult').hide();
                            }

                            //打印
                            $('#su').on('click',function () {
                                window.open('../../../print/opera/OperaAppForm/OperaAForm.html?rgstnum='+rgstnum);
                            });

                        },
                        yes: function(index) {
                            console.log(index);
                        },
                        full: function(elem) {
                            var win = window.top === window.self ? window : parent.window;
                            $(win).on('resize', function() {
                                var $this = $(this);
                                elem.width($this.width()).height($this.height()).css({
                                    top: 0,
                                    left: 0
                                });
                                elem.children('div.layui-layer-content').height($this.height() - 95);
                            });
                        }
                    });
                    form.render();
                });

            },
            error: function (e) {

            }
        });

    });


    $('body').on('click', '#close', function(){
        layer.close(index);
        return false;
    });

    $('body').on('click', '#closex', function(){
        layer.close(index);
        return false;
    });

    $('#search').on('click', function() {
        var type = $('#search_type').val();
        var value = $('#search_value').val();
        $('#s_type').attr("value",type);
        $('#s_value').attr("value",value);
        if(value === '' || value.length === 0) {
            layer.msg('请输入关键字！',{
                time:2000
            });
            return ;
        }

        paging.get({
            value: value,
            type: type
        });
    });

    $('#showall').on('click', function() {
        $('#search_type').val("");
        $('#search_value').val("");
        $('#s_type').removeAttr("value");
        $('#s_value').removeAttr("value");
        form.render();
        paging.get({
            value: null,
            type: null
        });
    });

    $('.site-table tbody tr').on('click', function(event) {
        var $this = $(this);
        var $input = $this.children('td').eq(0).find('input');
        $input.on('ifChecked', function(e) {
            $this.css('background-color', '#EEEEEE');
        });
        $input.on('ifUnchecked', function(e) {
            $this.removeAttr('style');
        });
        $input.iCheck('toggle');
    }).find('input').each(function() {
        var $this = $(this);
        $this.on('ifChecked', function(e) {
            $this.parents('tr').css('background-color', '#EEEEEE');
        });
        $this.on('ifUnchecked', function(e) {
            $this.parents('tr').removeAttr('style');
        });
    });

    exports('OperaItemsList', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});